package com.market.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.market.entity.Store;
import com.market.service.StoreService;
import com.market.utils.TokenUtil;
import com.market.vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

@RestController
public class StoreController {
    @Autowired
    private StoreService storeService;

    @GetMapping("storeLogin")
    public R storeLogin(String accountNumber, String password, HttpServletResponse resp){
        Store store = storeService.storeLogin(accountNumber, password);
        if (store==null){
            return new R(310,"用户名或密码错误",null);
        }else if (accountNumber==null){
            return new R(311,"账号不能为空",null);
        }else if (password==null){
            return new R(312,"密码不能为空",null);
        }
        String token = TokenUtil.getToken(store.getId(), 1000 * 60 * 60 * 24, "store");
        resp.setHeader("token",token);
        return new R(200,"登录成功",null);
    }

    @RequestMapping("findStore")
    public R<Store> findStore(HttpServletRequest resp){
        String token=resp.getHeader("token");
        Map<String, Object> map = TokenUtil.parseToken(token);
        String storeId=(String)map.get("id");
        int id=Integer.parseInt(storeId);
        if (storeService.findStore(id)==null){
            return new R<>(300,"查询不到该店铺信息",new Store());
        }
        return R.querySuccess(storeService.findStore(id));
    }

    @RequestMapping("apply")
    public R applyToStore(@RequestBody Store store){
        boolean b = storeService.addStore(store);
        return new R(b?201:301,b?"申请成功":"申请失败",null);
    }

    //修改商家的状态
    @RequestMapping("passAudit")
    public R pass(int id,int state){
        boolean audit = storeService.modifyState(id,state);
        if (!audit){
            return new R(302,"申请不通过",null);
        }
        return new R(202,"申请通过",null);
    }


    // 提现
    @RequestMapping("cash")
    public R cash(HttpServletRequest req, double money){
        String token = req.getHeader("token");
        Map<String, Object> map = TokenUtil.parseToken(token);
        boolean res = storeService.cashAdvance((int)map.get("id"), money);
        if (!res){
            return new R(303,"提现失败",null);
        }
        return new R(203,"提现成功",null);
    }

    @RequestMapping("findAllStore")
    public R findAllStore(int currentPage,int pageSize){
        PageHelper.startPage(currentPage, pageSize);
        List<Store> allStore = storeService.findAllStore();
        return new R(208,"查询所有小店成功",new PageInfo<>(allStore));
    }
}
